package com.example.transaction_service_b.config;

import com.example.transaction_service_b.common.Receiver;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * Created by Administrator on 2017/5/18.
 */
@Configuration
@EnableCaching(proxyTargetClass = true)
public class RabbitConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Bean
    public PlatformTransactionManager transactionManager(){
        return new RabbitTransactionManager(rabbitTemplate.getConnectionFactory());
    }

    @Autowired
    public Receiver receiver;

    /**
     * 指定Receiver类监听hello队列
     * @return
     */
    @Bean
    public SimpleMessageListenerContainer messageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(rabbitTemplate.getConnectionFactory());
        container.setTransactionManager(transactionManager());
        container.setChannelTransacted(true);
        container.setQueueNames("hello");
        container.setMessageListener(new MessageListenerAdapter(receiver));
        return container;
    }
}
